package com.placement.app.action;

import java.util.ArrayList;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;



import com.placement.app.forms.UserForm;
import com.placement.app.model.ScreenUserBVO;
import com.placement.app.service.UserService;
import com.placement.app.dao.UserDAO;
import com.placement.app.util.Constants;


public class UserAction extends BaseAction {
  	

	private static final Log logger = LogFactory.getLog(UserAction.class);

	private UserService userService;
	private UserDAO userDAO;

	
	public ActionForward load(ActionMapping mapping,ActionForm form,
							HttpServletRequest request,HttpServletResponse response){
		String forward="loadUser";
		getLoggedInUser(request);
		System.out.println("loading first page");
		logger.debug(">>loading first page");
		HttpSession session=request.getSession();
		UserForm userForm = (UserForm)form;
		ScreenUserBVO user = new ScreenUserBVO();		
		userForm.setScreenUserBVO(user);
		request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
		return mapping.findForward(forward);
	}	
	
	public ActionForward showlogin(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response){
		String forward="loadUser";
		System.out.println("login");
		UserForm userForm = (UserForm)form;
		ScreenUserBVO user = new ScreenUserBVO();
		user.setEmail("");
		userForm.setScreenUserBVO(user);
		request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.TRUE_FL);				
		request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
		return mapping.findForward(forward);
	}
	public ActionForward showcreatelogin(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
			String forward="loadUser";
			System.out.println("login");
			HttpSession session=request.getSession();
			UserForm userForm = (UserForm)form;
			ScreenUserBVO user = new ScreenUserBVO();
			
			request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.TRUE_FL);				
			request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
			return mapping.findForward(forward);
		}
	
	public ActionForward signin(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String forward="loadUser";			
		
		UserForm userForm = (UserForm)form;				
		ScreenUserBVO user = userForm.getScreenUserBVO();

		ActionMessages msgs = new ActionMessages();
		this.validateSignIn(user,msgs,request);
	
		if(msgs.isEmpty()){
			logger.debug(">>Logged in correctly");
			request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);				
			request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.TRUE_FL);
		}else{
			logger.debug(">>Invalid Loggin");
			request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.TRUE_FL);
			request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.FALSE_FL);	
			request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
			//userForm.setScreenUserBVO(null);
			saveErrors(request, msgs);				
		}
										
		return mapping.findForward(forward);
		}
	
	public ActionForward showall(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
			String forward="loadUser";
			request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.TRUE_FL);		
			request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
			return mapping.findForward(forward);
		}
	
	
	public ActionForward showuserinfo(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
			String forward="loadUser";
			long startTime = System.currentTimeMillis();
			forward=Constants.FORWARD_DISPLAY;			
			logger.debug("load method");						
			HttpSession session=request.getSession();
			UserForm userForm = (UserForm)form;				
			ScreenUserBVO user = userForm.getScreenUserBVO();			
			String reqUserId =user.getUserid();			
			//System.out.println(request.getAttribute(Constants.SES_USER_ID));
			logger.debug("request.getAttribute(Constants.SES_USER_ID):"+request.getSession().getAttribute(Constants.SES_USER_ID));
			if(reqUserId!=null){				
					userForm.setScreenUserBVO(userService.getUser(user));
					forward="loadUser";
			}else{
					forward="previousScreen";
			}
			forward="loadUser";			
			logger.debug(">>load>>ScreenUserBVO:"+userForm.getScreenUserBVO());
			
			request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
			request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.TRUE_FL);
			request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.TRUE_FL);
			return mapping.findForward(forward);
		}
	
	public ActionForward register(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
					
			String forward="loadUser";			
			
			UserForm userForm = (UserForm)form;				
			ScreenUserBVO user = userForm.getScreenUserBVO();

			ActionMessages msgs = new ActionMessages();
			this.validateCreateSignIn(user,msgs,request);
	
			if(msgs.isEmpty()){
				userService.registerUser(user);
				request.setAttribute(Constants.ALERT_MSG, Constants.MSG_SAVE_SUCCESS);
				logger.debug(">>Save New User Registration ");
				String reqUserId=userService.findUserId(user).getUserid();
				System.out.println("registration user id"+reqUserId);
				if(reqUserId!=null){				
						userForm.setScreenUserBVO(userService.getUser(user));
				}			
				request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
				request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
				request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
				request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.TRUE_FL);		
			}else{
				request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.TRUE_FL);
				request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.TRUE_FL);
				request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
				request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.FALSE_FL);		
				//userForm.setScreenUserBVO(null);
				saveErrors(request, msgs);				
			}
											
			return mapping.findForward(forward);
		}
	
	public ActionForward save(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		System.out.println("inside save");
		String forward="loadUser";
		UserForm userForm = (UserForm)form;				
		ScreenUserBVO user = userForm.getScreenUserBVO();			
		String reqUserId =user.getUserid();			
		userService.saveUser(user);
		request.setAttribute(Constants.SHOW_SIGN_IN, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_CREATE_LOGIN, com.placement.app.util.Constants.FALSE_FL);
		request.setAttribute(Constants.SHOW_ALL, com.placement.app.util.Constants.TRUE_FL);		
		request.setAttribute(Constants.SHOW_USERINFORMATION, com.placement.app.util.Constants.FALSE_FL);
		return mapping.findForward(forward);
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private void validateCreateSignIn(ScreenUserBVO user,ActionMessages msgs,HttpServletRequest request){		
		
		if ((user.getEmail()!= null && user.getEmail()!="") && userService.checkUserExist(user)){
	    	msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_ALREADYREGISTERED,""));
			request.setAttribute("ERR_MSG_LOGIN_ALREADYREGISTERED", "ALREADYREGISTERED");
		};
		
		if (user.getEmail()== null || user.getEmail()==""){
			msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_EMAIL,""));
			request.setAttribute("ERR_MSG_LOGIN_EMAIL", "EMAIL");
	    };
	    
	    if (user.getPassword()== null || user.getPassword()==""  ){	      
	        msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_PASSWORD,""));
	        request.setAttribute("ERR_MSG_LOGIN_PASSWORD", "password");
	    };	
		
	    if (!user.getPassword().equals(user.getConfirmPassword())){	      
	        msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_CONFIRMPASSWORD,""));
	        request.setAttribute("ERR_MSG_LOGIN_CONFIRMPASSWORD", "confirm	password");
	    };	
	}
	
	
private void validateSignIn(ScreenUserBVO user,ActionMessages msgs,HttpServletRequest request){		
		
		
		if (user.getEmail()== null || user.getEmail()==""){
			msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_EMAIL,""));
			request.setAttribute("ERR_MSG_LOGIN_EMAIL", "EMAIL");
	    };
	    
	    if ((user.getEmail()!= null && user.getEmail()!="") && !userService.checkUserExist(user)){
	    	msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGINID_NOTFOUND,""));
			request.setAttribute("ERR_MSG_LOGINID_NOTFOUND", "ERR_MSG_LOGINID_NOTFOUND");
		};
	    
	    if (user.getPassword()== null || user.getPassword()==""  ){	      
	        msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_PASSWORD,""));
	        request.setAttribute("ERR_MSG_LOGIN_PASSWORD", "password");
	    };    
	     String strUserid=userService.findUserId(user).getUserid();
	     System.out.println(strUserid);
	     request.setAttribute(Constants.SES_USER_ID,strUserid);
	     System.out.println("User id"+request.getAttribute(Constants.SES_USER_ID));
	    if (user.getPassword()!= null && user.getPassword() !="" && (strUserid ==null || strUserid =="") ){	      
	        msgs.add(Constants.VALIDATION_MSG,new ActionMessage(Constants.ERR_MSG_LOGIN_PASSWORD,""));
	        request.setAttribute("ERR_MSG_LOGIN_PASSWORD", "password");
	    }
	    	    	   
	    };    
				
}
